/*
    YmsgIRC - Program enabling YMSG chat clients to use IRC
    Copyright (C) 2011 Crash Daemonicus ( crashenator -at- gmail.com )

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "common.h"
#include "sockets_cross.h"
int XSABlock(SOCKET& s,bool block){
    //dbg_head("XSABlock(SOCKET& s,bool block)");
    #ifdef _WIN32
        u_long uNBlock=1;
        if(block) uNBlock=0;
        //ReturnD(ioctlsocket(s,FIONBIO,&uNBlock));
        return ioctlsocket(s,FIONBIO,&uNBlock);
    #else
        int flags = fcntl(s, F_GETFL, 0);
        if(block) UNSET_VAR(flags,O_NONBLOCK);
        else flags|=O_NONBLOCK;
        //ReturnD(fcntl(s, F_SETFL, flags));
        return fcntl(s, F_SETFL, flags);
    #endif
}

bool XSASinFromName(const string& name, sockaddr_in& sin_out)
{
    //dbg_head("XSASinFromName(string name, sockaddr_in& sin_out)");
    //dbgline(name);
    hostent* hst=gethostbyname(name.c_str());
    if(hst!=0){
        if (hst->h_addrtype == AF_INET) {
            sin_out.sin_family = AF_INET;
            sin_out.sin_addr.s_addr = *(u_long *) hst->h_addr_list[0];
            return true;
            //ReturnD(true);
        }
    }
    dbgline("E:");dbg(itos(XSAGetLastError()));
    return false;
    //ReturnD(false);
}
